<?php
namespace App\Models;

class Charge extends BaseModel
{
    protected $table = "charge";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }

    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 获取和
     */
    public function getCount($where)
    {
        return $this->multiWhere($where)->count();
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    public function card()
    {
        return $this->hasOne('App\Models\Card', 'card_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id', 'id');
    }

    public function getChargeInfoById($id)
    {
        return $this->with('user')->multiWhere(['id' => $id])->first();
    }

    /**
     *
     * @return mixed 充值记录
     */
    public function charge_log()
    {
        $where = [];
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 20);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 20);
        }
        return $rows;
    }

    /**
     * 改变预约状态
     */
    public function dealOrder1($id)
    {
        $this->start();
        try {
            $updata['status'] = 1;
            $updata['pay_time'] = time();
            $where['id'] = $id;
            $this->up($where, $updata);
            $data['id'] = $id;
            $data['wxbillno'] = 123123123;
//            $result = apiCurl('memberRecharge', $data);
//            if (!$result['result']) {
//                apiCurl('memberRecharge', $data);
//            }
            $payLog = new PayLog();
            $payLog->addPayLog(1, 0, 0, $id);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->rollBack();
            return false;
        }
    }

    /**
     * 改变充值状态
     */
    public function dealOrder($charge, $wxbillno)
    {
        $this->start();
        try {
            $updata['status'] = 1;
            $updata['pay_time'] = time();
            $where['id'] = $charge['id'];
            $this->up($where, $updata);

            $member = new Member();
            $card = new Card();
            $cardId = $charge['card_id'];
            $memberInfo = $member->getMemberInfoById($charge['member_id']);
            //无卡用户进行发卡操作
            if (!$memberInfo['card']) {
                $param['sca01'] = $memberInfo['reffer_id'];
                $param['cardno'] = $charge['card_no'];
                $param['amounts'] = $charge['money'];
                $param['billno'] = $charge['charge_no'];
                $param['wxbillno'] = $wxbillno;
                $param['wxpaytime'] = date('Y-m-d H:i:s',$updata['pay_time']);
                $param['wxopenid'] = $memberInfo['user']['openid'];
                $hiscard = apiCurl('grantCard', $param);
                if ($hiscard['Result']) {
                    $cdata = [
                        'user_id' => $charge['user_id'],
                        'member_id' => $charge['member_id'],
                        'name' => $memberInfo['name'],
                        'card_no' => $charge['card_no'],
                        'reffer_id' => $hiscard['VAA01'],
                        'created_at' => date('Y-m-d H:i:s',time()),
                    ];
                    $cardId = $card->add($cdata);
                }
            } else {
                $data['id'] = $charge['id'];
                $data['wxbillno'] = $wxbillno;
                //his接口充值
                $result = apiCurl('memberRecharge', $data);
                if (!$result['Result']) {
                    apiCurl('memberRecharge', $data);
                }
            }



            //根据充值金额升级会员等级
            $setting = new Setting();
            $group = $setting->getSetting('promotion');
            if ($charge['money'] >= $group['level2'][0]) {
                //card表type 1-普通会员卡2-家庭会员卡主卡3-家庭会员卡副卡
                //card表level 1-普通客户2-普通会员6-家庭会员
                $newCdata['type'] = 1;
                $newCdata['level'] = 2;
                if ($charge['money'] >= $group['level3'][0]) {
                    $newCdata['type'] = 2;
                    $newCdata['level'] = 6;
                }
                $card->up(['id' => $cardId], $newCdata);
            }
            $payLog = new PayLog();
            $payLog->addPayLog($charge['user_id'], 1, 0, $charge['id']);
            $this->commit();
            //短信类型:1预约成功,2绑定就诊人,3改签,4充值成功
//            $sms = new SmsLog();
//            $sms->sendCaptcha($memberInfo['mobile'],4, $charge);
            return true;
        } catch (\Exception $e) {
            $this->rollBack();
            return false;
        }
    }

    /**
     * 微信会员充值
     * @param $cardno    会员卡号
     * @param $amount    充值金额
     * @param $billno    充值单号
     * @param $wxbillno  微信官方支付单号
     * @param $wxpaytime 微信官方支付时间
     * @param $wxopenid  用户openid
     * return
     * Result 1成功0失败
     * Errmsg 失败信息/错误原因
     *
     */
    public function his_member_recharge($param)
    {
        $charge = $this->getChargeInfoById($param['id']);
        $data['cardno'] = $charge['card_no'];
        $data['amount'] = $charge['money'];
        $data['billno'] = $charge['charge_no'];
        $data['wxbillno'] = $param['wxbillno'];
        $data['wxpaytime'] = date('Y-m-d H:i:s',$charge['pay_time']);
        $data['wxopenid'] = $charge['user']['openid'];
        return $data;
    }


}